package com.wn.mine.repository

import androidx.lifecycle.LiveData
import com.wn.common.model.SingleLiveData
import com.wn.common.net.config.SP_KEY_USER_TOKEN
import com.wn.common.utils.CnSpUtils
import com.wn.service.db.UserDao
import com.wn.service.db.UserInfo
import dagger.hilt.android.scopes.ViewModelScoped
import javax.inject.Inject

/**
 * @Author: wn
 * @Date: 2023/5/31 10:07
 * @Description: 个人中心模块相关的 仓库  类实现
 */
@ViewModelScoped
class MineRepository @Inject constructor(private val userDao: UserDao) : IMineRepository {

  private val _liveUserInfo = SingleLiveData<UserInfo?>()

  override val liveUserInfo: LiveData<UserInfo?> = _liveUserInfo

  /**
   * 获取 userinfo的 dao 操作
   */
  override suspend fun getUserInfo() {
    userDao.queryUser().also {
      _liveUserInfo.value = it
    }
  }

  /**
   * 删除 userinfo的 dao 操作
   * 删除 token
   */
  override suspend fun deleteUserAndToken(userInfo: UserInfo) {
    userDao.deleteUser(userInfo)
    CnSpUtils.remove(SP_KEY_USER_TOKEN)
  }
}

